<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>FlowEdge.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="FlowEdge code in Java">
<meta NAME="TITLE" CONTENT="FlowEdge code in Java">
<meta NAME="KEYWORDS" CONTENT="FlowEdge,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>FlowEdge.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "FlowEdge.java">FlowEdge.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac FlowEdge.java</span>
<span class="comment"> *  Execution:    java FlowEdge</span>
<span class="comment"> *  Dependencies: StdOut.java</span>
<span class="comment"> *</span>
<span class="comment"> *  Capacitated edge with a flow in a flow network.</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">FlowEdge</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class represents a capacitated edge with a </span>
<span class="comment">  * flow in a {</span><span class="type">@link</span><span class="comment"> FlowNetwork}. Each edge consists of two integers</span>
<span class="comment"> *  (naming the two vertices), a real-valued capacity, and a real-valued</span>
<span class="comment"> *  flow. The data type provides methods for accessing the two endpoints</span>
<span class="comment"> *  of the directed edge and the weight. It also provides methods for</span>
<span class="comment"> *  changing the amount of flow on the edge and determining the residual</span>
<span class="comment"> *  capacity of the edge.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation, see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/64maxflow"</span><span class="keyword">&gt;</span><span class="comment">Section 6.4</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">FlowEdge</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">int</span><span class="normal"> v</span><span class="symbol">;</span><span class="normal">             </span><span class="comment">// from</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">int</span><span class="normal"> w</span><span class="symbol">;</span><span class="normal">             </span><span class="comment">// to </span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">double</span><span class="normal"> capacity</span><span class="symbol">;</span><span class="normal">   </span><span class="comment">// capacity</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">double</span><span class="normal"> flow</span><span class="symbol">;</span><span class="normal">             </span><span class="comment">// flow</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Initializes an edge from vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">w</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> with</span>
<span class="comment">     * the given </span><span class="keyword">&lt;tt&gt;</span><span class="comment">capacity</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and zero flow.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> v the tail vertex</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> w the head vertex</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> capacity the capacity of the edge</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> java.lang.IndexOutOfBoundsException if either </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> or </span><span class="keyword">&lt;tt&gt;</span><span class="comment">w</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     *    is a negative integer</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> java.lang.IllegalArgumentException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">capacity</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">FlowEdge</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> w</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> capacity</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">v </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IndexOutOfBoundsException</span><span class="symbol">(</span><span class="string">"Vertex name must be a nonnegative integer"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">w </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IndexOutOfBoundsException</span><span class="symbol">(</span><span class="string">"Vertex name must be a nonnegative integer"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">capacity </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Edge capacity must be nonnegaitve"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">v         </span><span class="symbol">=</span><span class="normal"> v</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">w         </span><span class="symbol">=</span><span class="normal"> w</span><span class="symbol">;</span><span class="normal">  </span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">capacity  </span><span class="symbol">=</span><span class="normal"> capacity</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">flow      </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Initializes an edge from vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">w</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> with</span>
<span class="comment">     * the given </span><span class="keyword">&lt;tt&gt;</span><span class="comment">capacity</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">flow</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> v the tail vertex</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> w the head vertex</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> capacity the capacity of the edge</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> flow the flow on the edge</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> java.lang.IndexOutOfBoundsException if either </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> or </span><span class="keyword">&lt;tt&gt;</span><span class="comment">w</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     *    is a negative integer</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> java.lang.IllegalArgumentException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">capacity</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is negative</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> java.lang.IllegalArgumentException unless </span><span class="keyword">&lt;tt&gt;</span><span class="comment">flow</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is between </span>
<span class="comment">     *    </span><span class="keyword">&lt;tt&gt;</span><span class="comment">0.0</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">capacity</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">FlowEdge</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> w</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> capacity</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> flow</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">v </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IndexOutOfBoundsException</span><span class="symbol">(</span><span class="string">"Vertex name must be a nonnegative integer"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">w </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IndexOutOfBoundsException</span><span class="symbol">(</span><span class="string">"Vertex name must be a nonnegative integer"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">capacity </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">))</span><span class="normal">  </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Edge capacity must be nonnegaitve"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">flow </span><span class="symbol">&lt;=</span><span class="normal"> capacity</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Flow exceeds capacity"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">flow </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">))</span><span class="normal">      </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Flow must be nonnnegative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">v         </span><span class="symbol">=</span><span class="normal"> v</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">w         </span><span class="symbol">=</span><span class="normal"> w</span><span class="symbol">;</span><span class="normal">  </span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">capacity  </span><span class="symbol">=</span><span class="normal"> capacity</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">flow      </span><span class="symbol">=</span><span class="normal"> flow</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Initializes a flow edge from another flow edge.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> e the edge to copy</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">FlowEdge</span><span class="symbol">(</span><span class="usertype">FlowEdge</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">v         </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">.</span><span class="normal">v</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">w         </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">.</span><span class="normal">w</span><span class="symbol">;</span><span class="normal">  </span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">capacity  </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">.</span><span class="normal">capacity</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">flow      </span><span class="symbol">=</span><span class="normal"> e</span><span class="symbol">.</span><span class="normal">flow</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the tail vertex of the edge.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the tail vertex of the edge</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">from</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> v</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span><span class="normal">  </span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the head vertex of the edge.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the head vertex of the edge</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">to</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> w</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span><span class="normal">  </span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the capacity of the edge.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the capacity of the edge</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">capacity</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> capacity</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the flow on the edge.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the flow on the edge</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">flow</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> flow</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the endpoint of the edge that is different from the given vertex</span>
<span class="comment">     * (unless the edge represents a self-loop in which case it returns the same vertex).</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> vertex one endpoint of the edge</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the endpoint of the edge that is different from the given vertex</span>
<span class="comment">     *   (unless the edge represents a self-loop in which case it returns the same vertex)</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> java.lang.IllegalArgumentException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">vertex</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is not one of the endpoints</span>
<span class="comment">     *   of the edge</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">other</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> vertex</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal">      </span><span class="symbol">(</span><span class="normal">vertex </span><span class="symbol">==</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> w</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">vertex </span><span class="symbol">==</span><span class="normal"> w</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> v</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Illegal endpoint"</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the residual capacity of the edge in the direction</span>
<span class="comment">     *  to the given </span><span class="keyword">&lt;tt&gt;</span><span class="comment">vertex</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> vertex one endpoint of the edge</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the residual capacity of the edge in the direction to the given vertex</span>
<span class="comment">     *   If </span><span class="keyword">&lt;tt&gt;</span><span class="comment">vertex</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is the tail vertex, the residual capacity equals</span>
<span class="comment">     *   </span><span class="keyword">&lt;tt&gt;</span><span class="comment">capacity() - flow()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">; if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">vertex</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is the head vertex, the</span>
<span class="comment">     *   residual capacity equals </span><span class="keyword">&lt;tt&gt;</span><span class="comment">flow()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> java.lang.IllegalArgumentException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">vertex</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is not one of the endpoints</span>
<span class="comment">     *   of the edge</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">residualCapacityTo</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> vertex</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal">      </span><span class="symbol">(</span><span class="normal">vertex </span><span class="symbol">==</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> flow</span><span class="symbol">;</span><span class="normal">              </span><span class="comment">// backward edge</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">vertex </span><span class="symbol">==</span><span class="normal"> w</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> capacity </span><span class="symbol">-</span><span class="normal"> flow</span><span class="symbol">;</span><span class="normal">   </span><span class="comment">// forward edge</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Illegal endpoint"</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Increases the flow on the edge in the direction to the given vertex.</span>
<span class="comment">     *   If </span><span class="keyword">&lt;tt&gt;</span><span class="comment">vertex</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is the tail vertex, this increases the flow on the edge by </span><span class="keyword">&lt;tt&gt;</span><span class="comment">delta</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">;</span>
<span class="comment">     *   if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">vertex</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is the head vertex, this decreases the flow on the edge by </span><span class="keyword">&lt;tt&gt;</span><span class="comment">delta</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> vertex one endpoint of the edge</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> java.lang.IllegalArgumentException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">vertex</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is not one of the endpoints</span>
<span class="comment">     *   of the edge</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> java.lang.IllegalArgumentException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">delta</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> makes the flow on</span>
<span class="comment">     *   on the edge either negative or larger than its capacity</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> java.lang.IllegalArgumentException if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">delta</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is </span><span class="keyword">&lt;tt&gt;</span><span class="comment">NaN</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">addResidualFlowTo</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> vertex</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> delta</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal">      </span><span class="symbol">(</span><span class="normal">vertex </span><span class="symbol">==</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> flow </span><span class="symbol">-=</span><span class="normal"> delta</span><span class="symbol">;</span><span class="normal">           </span><span class="comment">// backward edge</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">vertex </span><span class="symbol">==</span><span class="normal"> w</span><span class="symbol">)</span><span class="normal"> flow </span><span class="symbol">+=</span><span class="normal"> delta</span><span class="symbol">;</span><span class="normal">           </span><span class="comment">// forward edge</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Illegal endpoint"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">Double</span><span class="symbol">.</span><span class="function">isNaN</span><span class="symbol">(</span><span class="normal">delta</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Change in flow = NaN"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">flow </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">))</span><span class="normal">      </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Flow is negative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">flow </span><span class="symbol">&lt;=</span><span class="normal"> capacity</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"Flow exceeds capacity"</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a string representation of the edge.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a string representation of the edge</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> </span><span class="function">toString</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> v </span><span class="symbol">+</span><span class="normal"> </span><span class="string">"-&gt;"</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> w </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> flow </span><span class="symbol">+</span><span class="normal"> </span><span class="string">"/"</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> capacity</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Unit tests the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">FlowEdge</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> data type.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">FlowEdge</span><span class="normal"> e </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">FlowEdge</span><span class="symbol">(</span><span class="number">12</span><span class="symbol">,</span><span class="normal"> </span><span class="number">23</span><span class="symbol">,</span><span class="normal"> </span><span class="number">3.14</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
